package com.jianz.campus_markets.controller;

import com.jianz.campus_markets.domain.ResponseResult;
import com.jianz.campus_markets.domain.entity.User;
import com.jianz.campus_markets.handler.exception.SystemException;
import com.jianz.campus_markets.service.LoginService;
import com.jianz.campus_markets.service.impl.LoginServiceImpl;
import com.jianz.campus_markets.utils.AppHttpCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author Jianz
 * @version 1.0
 * @Description
 * @Email jianz8153.gmail.com
 * @Date 2023/11/22 8:24
 */

@RestController
@Api(value = "登录注册模块")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/register")
    public ResponseResult register(@RequestBody User user) {
        return loginService.register(user);
    }

    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){

        // 提示必须传用户名
        if(!StringUtils.hasText(user.getUserName())){
            throw new SystemException(AppHttpCodeEnum.REQUIRE_USERNAME);
        }
        return loginService.login(user);
    }

    @PostMapping("/logout")
    public ResponseResult logout(){
        return loginService.logout();
    }

}
